package wangjg.commons.bean;

import java.util.Map;
import java.util.Map.Entry;

/**
 * 
 * @author wangjg
 *
 */
public class XmlBeanFactory {
	private Map<String,Object> beanMap ;
	
	public XmlBeanFactory(String xmlPath) {
		this.beanMap = XmlBeanParser.getContext(xmlPath);
	}
	
	public Object getBean(String name){
		return beanMap.get(name);
	}

	@SuppressWarnings("unchecked")
	public <T> T getBean(Class<T> requiredType) {
		for(Entry<String, Object> entry:beanMap.entrySet()){
			Object bean = entry.getValue();
			if(bean!=null && bean.getClass().isAssignableFrom(requiredType)){
				return (T)bean;
			}
		}
		return null;
	}

}
